﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Competences 的摘要说明
/// </summary>
public class Competences
{
    #region 权限常量   

    /// <summary>
    /// 管理网站会员的权限、控制、设置等权限。
    /// </summary>
    public const string Competence = "Competence";

    /// <summary>
    /// 填充网站的各种内容的权限。
    /// </summary>
    public const string FillContent = "FillContent";

    /// <summary>
    /// 编辑、发布网站的各种新闻资讯栏目；编辑网站公告的权限。
    /// </summary>
    public const string EditNews = "EditNews";

    /// <summary>
    /// 查询、修改会员信息的权限。
    /// </summary>
    public const string MemberManagement = "MemberManagement";

    /// <summary>
    /// 会员账户充值的权限。
    /// </summary>
    public const string AddMoney = "AddMoney";

    /// <summary>
    /// 接受、处理会员提款的权限。
    /// </summary>
    public const string DistillMoney = "DistillMoney";

    /// <summary>
    /// 查阅会员提问、反馈答复会员资讯、解决客户问题的权限。
    /// </summary>
    public const string MemberService = "MemberService";

    /// <summary>
    /// 会员群发邮件
    /// </summary>
    public const string SendMessage = "SendMessage";

    /// <summary>
    /// 发送手机短信的权限。
    /// </summary>
    public const string SendSMS = "SendSMS";

    /// <summary>
    /// 管理系统日志的权限。
    /// </summary>
    public const string Log = "Log";

    /// <summary>
    /// 网站各参数设置的高级权限。
    /// </summary>
    public const string Options = "Options";

    /// <summary>
    /// 查看网络系部图的权限。
    /// </summary>
    public const string Net = "Net";

    /// <summary>
    /// 代理商注册新会员的权限。
    /// </summary>
    public const string SurrogateR = "SurrogateR";

    /// <summary>
    /// 代理商激活新会员的权限。
    /// </summary>
    public const string SurrogateA = "SurrogateA";

    /// <summary>
    /// 代理商开通网店的权限。
    /// </summary>
    public const string SurrogateO = "SurrogateO";   

 
    #region 奖金结算管理(编号01-35) 

    /// <summary>
    /// 超级管理权限，拥有对网站的全部控制权。
    /// </summary>
    public const string Administrator="Administrator";

    /// <summary>
    /// 报单管理
    /// </summary>
    public const string BDGL_2="BDGL_2";

    /// <summary>
    /// 资金结算
    /// </summary>
    public const string ZJJS_3="ZJJS_3";

    /// <summary>
    /// 报表查询
    /// </summary>
    public const string BBCX_4="BBCX_4";

    /// <summary>
    /// 进销存管理
    /// </summary>
    public const string KCGL_5="KCGL_5";

    /// <summary>
    /// 系统设置|认证中心
    /// </summary>
    public const string RZZX_6="RZZX_6";

    /// <summary>
    /// 公司公告
    /// </summary>
    public const string GSGG_7="GSGG_7";

    /// <summary>
    /// 公告管理
    /// </summary>
    public const string GGGL_8="GGGL_8";

    /// <summary>
    /// 公司新闻
    /// </summary>
    public const string GSXW_9="GSXW_9";

    /// <summary>
    /// 新闻管理
    /// </summary>
    public const string XWGL_10="XWGL_10";

    /// <summary>
    /// 培训信息
    /// </summary>
    public const string PXXX_11="PXXX_11";

    /// <summary>
    /// 培训管理
    /// </summary>
    public const string PXGL_12="PXGL_12";

    /// <summary>
    /// 直接报单
    /// </summary>
    public const string ZJBD_13="ZJBD_13";

    /// <summary>
    /// 购物报单
    /// </summary>
    public const string GWBD_14="GWBD_14";

    /// <summary>
    /// 系谱图
    /// </summary>
    public const string XPT_15="XPT_15";

    /// <summary>
    /// 我的资料
    /// </summary>
    public const string WDZL_16="WDZL_16";

    /// <summary>
    /// 我的团队
    /// </summary>
    public const string WDTD_17="WDTD_17";

    /// <summary>
    /// 我的下线
    /// </summary>
    public const string WDXX_18="WDXX_18";

    /// <summary>
    /// 添加会员
    /// </summary>
    public const string TJHY_19="TJHY_19";

    /// <summary>
    /// 会员列表
    /// </summary>
    public const string HYLB_20="HYLB_20";

    /// <summary>
    /// 会员审核
    /// </summary>
    public const string HYSH_21="HYSH_21";

    /// <summary>
    /// 设置费点
    /// </summary>
    public const string SZFD_22="SZFD_22";

    /// <summary>
    /// 我的留言
    /// </summary>
    public const string WDLY_23="WDLY_23";

    /// <summary>
    /// 在线咨询
    /// </summary>
    public const string ZXZX_24="ZXZX_24";

    /// <summary>
    /// 群发邮件
    /// </summary>
    public const string QFYJ_25="QFYJ_25";

    /// <summary>
    /// 手机短信
    /// </summary>
    public const string SJDX_26="SJDX_26";

    /// <summary>
    /// 我的业绩
    /// </summary>
    public const string WDYJ_27="WDYJ_27";

    /// <summary>
    /// 在线充值
    /// </summary>
    public const string ZXCZ_28="ZXCZ_28";

    /// <summary>
    /// 手动充值
    /// </summary>
    public const string SDCZ_29="SDCZ_29";

    /// <summary>
    /// 充值记录
    /// </summary>
    public const string CZJL_30="CZJL_30";

    #endregion

    #region 财务管理中心(编号36-50) 

    /// <summary>
    /// 处理在线支付
    /// </summary>
    public const string CLZXZF_36="CLZXZF_36";

    /// <summary>
    /// 申请提款
    /// </summary>
    public const string SQTK_37="SQTK_37";

    /// <summary>
    /// 处理提款
    /// </summary>
    public const string CLTK_38="CLTK_38";

    /// <summary>
    /// 提款记录
    /// </summary>
    public const string TKJL_39="TKJL_39";

    /// <summary>
    /// 收入明细
    /// </summary>
    public const string SRMX_40="SRMX_40";

    /// <summary>
    /// 支出明细
    /// </summary>
    public const string ZCMX_41="ZCMX_41";

    /// <summary>
    /// 已发工资
    /// </summary>
    public const string YFGZ_42="YFGZ_42";

    /// <summary>
    /// 待发工资
    /// </summary>
    public const string DFGZ_43="DFGZ_43";

    /// <summary>
    /// 处理漏发
    /// </summary>
    public const string CLLF_44="CLLF_44";

    /// <summary>
    /// 是否允许进入财务模块
    /// </summary>
    public const string Finance = "Finance";

    #endregion

    #region 店铺进销存(编号51-80)

    /// <summary>
    /// 制作采购单
    /// </summary>
    public const string ZZCGD_51 = "ZZCGD_51";

    /// <summary>
    /// 审核采购单
    /// </summary>
    public const string SHCGD_52 = "SHCGD_52";

    /// <summary>
    /// 采购单查询
    /// </summary>
    public const string CGDCX_53 = "CGDCX_53";
    
    /// <summary>
    /// 制作要货单
    /// </summary>
    public const string ZZYHD_54="ZZYHD_54";

    /// <summary>
    /// 审核要货单
    /// </summary>
    public const string SHYHD_55="SHYHD_55";

    /// <summary>
    /// 要货单查询
    /// </summary>
    public const string YHDCX_56="YHDCX_56";

    /// <summary>
    /// 制作入库单
    /// </summary>
    public const string ZZRKD_57="ZZRKD_57";

    /// <summary>
    /// 审核入库单
    /// </summary>
    public const string SHRKD_59="SHRKD_59";

    /// <summary>
    /// 入库单查询
    /// </summary>
    public const string RKDCX_60="RKDCX_60";

    /// <summary>
    /// 制作出库单
    /// </summary>
    public const string ZZCKD_61="ZZCKD_61";

    /// <summary>
    /// 审核出库单
    /// </summary>
    public const string SHCKD_62="SHCKD_62";

    /// <summary>
    /// 出库单查询
    /// </summary>
    public const string CKDCX_63="CKDCX_63";

    /// <summary>
    /// 制作调拨单
    /// </summary>
    public const string ZZTBD_64="ZZTBD_64";

    /// <summary>
    /// 审核调拨单
    /// </summary>
    public const string SHDBD_65="SHDBD_65";

    /// <summary>
    /// 调拨单查询
    /// </summary>
    public const string DBDCX_66="DBDCX_66";

    /// <summary>
    /// 制作退货单
    /// </summary>
    public const string ZZTHD_67="ZZTHD_67";

    /// <summary>
    /// 审核退货单
    /// </summary>
    public const string SGTHD_68="SGTHD_68";

    /// <summary>
    /// 退货单查询
    /// </summary>
    public const string THDCX_69="THDCX_69";

    /// <summary>
    /// 制作损耗单
    /// </summary>
    public const string ZZSHD_70="ZZSHD_70";

    /// <summary>
    /// 审核损耗单
    /// </summary>
    public const string SHSHD_71="SHSHD_71";

    /// <summary>
    /// 查询损耗单
    /// </summary>
    public const string CXSHD_72="CXSHD_72";

    /// <summary>
    /// 品牌管理
    /// </summary>
    public const string PPGL_73="PPGL_73";

    /// <summary>
    /// 类别管理
    /// </summary>
    public const string LBGL_74="LBGL_74";

    /// <summary>
    /// 档案管理
    /// </summary>
    public const string DAGL_75="DAGL_75";

    /// <summary>
    /// 进销存模块
    /// </summary>
    public const string Jxc = "Jxc";

    #endregion

    #region 系统设置|认证中心(编号81-100)

    /// <summary>
    /// 密码修改
    /// </summary>
    public const string MMXG_81 = "MMXG_81";

    /// <summary>
    /// 系统注册
    /// </summary>
    public const string XTZC_82 = "XTZC_82";

    /// <summary>
    /// 选项设置
    /// </summary>
    public const string XXSZ_83 = "XXSZ_83";

    /// <summary>
    /// 用户登陆日志
    /// </summary>
    public const string YHDLRZ_84 = "YHDLRZ_84";

    /// <summary>
    /// 系统操作日志
    /// </summary>
    public const string XTCZRZ_85 = "XTCZRZ_85";

    /// <summary>
    /// 组权限管理
    /// </summary>
    public const string ZQXGL_86 = "ZQXGL_86";

    /// <summary>
    /// 会员权限管理
    /// </summary>
    public const string HYQXGL_87 = "HYQXGL_87";

    /// <summary>
    /// 备份数据库
    /// </summary>
    public const string BFSJK_88 = "BFSJK_88";

    /// <summary>
    /// 执行SQLScript
    /// </summary>
    public const string ZXSQL_89 = "ZXSQL_89";

    /// <summary>
    /// 认证中心｜系统设置模块
    /// </summary>
    public const string SetSystem = "SetSystem";

    #endregion

    #region 报表查询(编号101-120)

    /// <summary>
    /// 产品销售排行
    /// </summary>
    public const string CPXSPH_101 = "CPXSPH_101";

    /// <summary>
    /// 产品销售明细
    /// </summary>
    public const string CPXSMX_102 = "CPXSMX_102";

    /// <summary>
    /// 产品销售跟踪
    /// </summary>
    public const string CPXSGZ_103 = "CPXSGZ_103";

    /// <summary>
    /// 产品明细
    /// </summary>
    public const string CPMX_104 = "CPMX_104";

    /// <summary>
    /// 产品退货库存分布情况
    /// </summary>
    public const string CPTHKCFBQK_105 = "CPTHKCFBQK_105";

    /// <summary>
    /// 总仓库入库情况
    /// </summary>
    public const string ZCKRKQK_106 = "ZCKRKQK_106";

    /// <summary>
    /// 调拨管理情况
    /// </summary>
    public const string DBGLQK_107 = "DBGLQK_107";

    /// <summary>
    /// 商品滞销情况
    /// </summary>
    public const string SPZXQK_108 = "SPZXQK_108";

    #endregion

    #region 商城网店(编号121-140)

    #endregion

    /// <summary>
    /// 商城网店模块
    /// </summary>
    public const string SiteShop = "SiteShop";


    #endregion

    public Customer Customer;

    public string CompetencesList
    {
        get
        {
            if ((Customer == null) || (Customer.ID < 1))
            {
                throw new Exception("没有初始化 Competences 类的 Customer 变量");
            }

            return DAL.Functions.F_GetCustomerCompetencesList(Customer.ID);
        }
    }

    public Competences()
    {
        Customer = null;
    }

    public Competences(Customer customer)
    {
        Customer = customer;
    }

    public bool this[string CompetenceName]
    {
        get
        {
            if ((Customer == null) || (Customer.ID < 1))
            {
                throw new Exception("没有初始化 Competences 类的 Customer 变量");
            }

            return (DAL.Functions.F_GetCustomerCompetencesList(Customer.ID).IndexOf("[" + CompetenceName + "]") >= 0);
        }
    }

    /// <summary>
    ///  设置用户权限
    /// </summary>
    /// <param name="CompetencesList"> 权限列表 </param>
    /// <param name="GroupsList"> 组列表 </param>
    /// <param name="ReturnDescription"> 返回值 </param>    
    public int SetCustomerCompetences(string CompetencesList, string GroupsList, ref string ReturnDescription)
    {
        if ((Customer == null) || (Customer.ID < 1))
        {
            throw new Exception("没有初始化 Competences 类的 Customer 变量");
        }

        int ReturnValue = 0;
        ReturnDescription = "";

        int Result = DAL.Procedures.P_SetCustomerCompetences(Customer.ID, CompetencesList, GroupsList, Customer.SiteID, ref ReturnDescription);

        if (Result < 0)
        {
            ReturnDescription = "数据库读写错误";

            return -1;
        }

        if (ReturnValue < 0)
        {
            return ReturnValue;
        }

        return 0;
    }

    /// <summary>
    /// 是否拥有权限列表需要的权限
    /// </summary>
    /// <param name="RequestCompetencesList"> 需要匹配的权限列表 </param>    
    public bool IsOwnedCompetences(string RequestCompetencesList)
    {
        if ((Customer == null) || (Customer.ID < 1))
        {
            throw new Exception("没有初始化 Competences 类的 Customer 变量");
        }

        string UserCompetencesList = CompetencesList;

        if (UserCompetencesList.IndexOf("[" + Competences.Administrator + "]") >= 0)    // 拥有超级权限
        {
            return true;
        }

        RequestCompetencesList = RequestCompetencesList.Trim();

        if (RequestCompetencesList == "")
        {
            return true;
        }

        RequestCompetencesList = RequestCompetencesList.Replace("][", ",");
        RequestCompetencesList = RequestCompetencesList.Substring(1, RequestCompetencesList.Length - 2);

        string[] strs = RequestCompetencesList.Split(',');

        bool HaveThisCompetences = false;

        foreach (string str in strs)
        {
            if ((UserCompetencesList.IndexOf("[" + str + "]") >= 0) && (!HaveThisCompetences))
            {
                HaveThisCompetences = true;
            }
        }

        return HaveThisCompetences;
    }

    /// <summary>
    ///  构建需要的权限列表字串
    /// </summary>
    /// <param name="CompetenceList"> 权限列表</param>
    /// <returns> [a][b]... </returns>
    public static string BuildCompetencesList(params string[] CompetenceList)
    {
        string Result = "";

        foreach (string str in CompetenceList)
        {
            Result += "[" + str + "]";
        }

        return Result;
    }

}